﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System.Data.SugarClient.Soap.V1
{

	internal abstract class V1Action : ISugarSoapAction
	{

		#region Constructors

		internal V1Action(
			SugarSoapV1.sugarsoap soapService)
		{
			this._soapservice = soapService;
		}

		#endregion

		#region Fields

		private readonly SugarSoapV1.sugarsoap _soapservice;

		#endregion

		#region Properties

		public SugarSoapV1.sugarsoap SoapService
		{
			get { return _soapservice; }
		}

		#endregion

		
		#region Methods

		public ISugarSoapActionResult Execute()
		{
			Trace.Debug("{0}.Execute()", this.GetType().Name);
			try
			{
				return this.ExecuteInner();
			}
			catch (System.InvalidOperationException ex)
			{
				Trace.Debug(ex.Message);
				Trace.Debug(ex.StackTrace);
				// sugar soap returns TEXT/HTML content when something is wrong with this operation.
				// Because .NET framework expects that TEXT/XML content will return from server, InvalidOperationException is invoked.
				throw new SugarException("Exception while communicating with server", ex);
			}
			catch (Exception ex)
			{
				Trace.Debug("Unhandled Exception Occured while Execute(). {0}", ex.Message);
				Trace.Debug("{0}", ex.StackTrace);

				throw new SugarException("Unhandled Exception", ex);
			}
		}

		protected virtual V1ActionResult ExecuteInner()
		{
			throw new NotImplementedException();
		}

		#endregion

	}

	internal abstract class V1ActionResult : ISugarSoapActionResult
	{

		#region Constructors

		internal V1ActionResult(
			V1Action action)
		{
			this._action = action;
		}

		#endregion

		#region Fields

		private readonly V1Action _action;

		#endregion

		#region Propreties

		public virtual bool Success
		{
			get { return false; }
		}

		public ISugarSoapAction Action
		{
			get { return _action; }
		}

		#endregion

	}

	
}
